package com.hc.sort.manager;

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import com.hc.sort.server.pojo.*;
import com.hc.sort.server.service.*;
import java.awt.event.*;
import java.util.List;

public class BusinessTypeAddDlg extends JDialog {
	//持有父窗口的实例，在保存的时候，可以刷新数据
	ManagerUI parentUI;
	
	//业务层的对象
	BusinessTypeService businessTypeService=new BusinessTypeServiceImpl();

	private final JPanel contentPanel = new JPanel();
	private JTextField textFieldBusLimit;
	private JTextField textFieldBusName;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			BusinessTypeAddDlg dialog = new BusinessTypeAddDlg();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	

	public BusinessTypeAddDlg(ManagerUI parentUI) {
		this();
		this.parentUI = parentUI;
	}



	/**
	 * Create the dialog.
	 */
	private BusinessTypeAddDlg() {
		setTitle("添加业务类型");
		setBounds(100, 100, 472, 356);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		
		JLabel labelBusCode = new JLabel("业务代码：");
		labelBusCode.setFont(new Font("宋体", Font.PLAIN, 15));
		labelBusCode.setBounds(57, 23, 79, 29);
		contentPanel.add(labelBusCode);
		
		JLabel labelBusName = new JLabel("业务名称：");
		labelBusName.setFont(new Font("宋体", Font.PLAIN, 15));
		labelBusName.setBounds(57, 72, 79, 29);
		contentPanel.add(labelBusName);
		
		JLabel labelBusLimit = new JLabel("业务上限：");
		labelBusLimit.setFont(new Font("宋体", Font.PLAIN, 15));
		labelBusLimit.setBounds(57, 115, 79, 29);
		contentPanel.add(labelBusLimit);
		
		JLabel labelBusDesc = new JLabel("业务备注：");
		labelBusDesc.setFont(new Font("宋体", Font.PLAIN, 15));
		labelBusDesc.setBounds(57, 162, 79, 29);
		contentPanel.add(labelBusDesc);
		
		JComboBox cBoxBusCode = new JComboBox();
		cBoxBusCode.setFont(new Font("宋体", Font.PLAIN, 14));
		cBoxBusCode.setModel(new DefaultComboBoxModel(new String[] {"A", "B", "C", "D", "E", "F"}));
		cBoxBusCode.setBounds(156, 27, 202, 21);
		contentPanel.add(cBoxBusCode);
		
		JTextArea textAreaDesc = new JTextArea();
		textAreaDesc.setFont(new Font("Monospaced", Font.PLAIN, 14));
		textAreaDesc.setBounds(156, 164, 202, 77);
		contentPanel.add(textAreaDesc);
		
		textFieldBusLimit = new JTextField();
		textFieldBusLimit.setFont(new Font("宋体", Font.PLAIN, 14));
		textFieldBusLimit.setBounds(156, 118, 202, 21);
		contentPanel.add(textFieldBusLimit);
		textFieldBusLimit.setColumns(10);
		
		textFieldBusName = new JTextField();
		textFieldBusName.setFont(new Font("宋体", Font.PLAIN, 14));
		textFieldBusName.setColumns(10);
		textFieldBusName.setBounds(156, 72, 202, 21);
		contentPanel.add(textFieldBusName);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						//保存数据
						BusinessType busType=new BusinessType();
						
						String busCode=cBoxBusCode.getSelectedItem().toString();
						String busName=textFieldBusName.getText();
						String limit=textFieldBusLimit.getText();
						String desc=textAreaDesc.getText();
						
						//校验
						int flage=1;
						List<BusinessType> busList=businessTypeService.findAll();
						for (int j = 0; j < busList.size(); j++) {
							if(busCode!=null && busCode.equals(busList.get(j).getCode())) {
								JOptionPane.showMessageDialog(null, "业务代码不能重复，请重新选择");
								flage=0;
							}
						}
						
						if (flage==1) {
							if (busName.equals("")) {
								JOptionPane.showMessageDialog(null, "业务名称不能为空，请重新输入");
							}else if (limit.equals("")) {
								JOptionPane.showMessageDialog(null, "业务上限不能重复，请重新输入");
							}else {
								busType.setCode(busCode);
								busType.setName(busName);
								busType.setDesc(desc);
								busType.setLimitCount(Integer.parseInt(limit));
								
								int ret=businessTypeService.add(busType);
								if (ret>0) {
									JOptionPane.showMessageDialog(null, "保存成功");
									BusinessTypeAddDlg.this.parentUI.flushData();
									
									//关闭对话框
									BusinessTypeAddDlg.this.dispose();
								}else {
									JOptionPane.showMessageDialog(null, "保存失败");
								}
							}
						}
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						BusinessTypeAddDlg.this.dispose();
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}
}
